package com.fable.base;

/**
 * 业务异常
 */
public class ComponentException extends RuntimeException {

    private static final long serialVersionUID = 6757986084644052746L;
    private ErrCode errCode;

    /**
     * 异常码
     */
    public interface ErrCode {

        ErrCode 系统异常 = new ErrCode() {

            @Override
            public String code() {
                return "F-0000";
            }

            @Override
            public String name() {
                return "系统异常";
            }
        };
        ErrCode 参数非法 = new ErrCode() {

            @Override
            public String code() {
                return "F-0001";
            }

            @Override
            public String name() {
                return "参数非法";
            }
        };
        ErrCode 数据错误 = new ErrCode() {

            @Override
            public String code() {
                return "F-0002";
            }

            @Override
            public String name() {
                return "数据错误";
            }
        };

        ErrCode 操作非法 = new ErrCode() {

            @Override
            public String code() {
                return "FER-0003";
            }

            @Override
            public String name() {
                return "操作非法";
            }
        };

        ErrCode 性能不良 = new ErrCode() {

            @Override
            public String code() {
                return "F-0004"; // 监控
            }

            @Override
            public String name() {
                return "性能不良";
            }
        };

        ErrCode 加锁失败 = new ErrCode() {

            @Override
            public String code() {
                return "F-0005"; // 监控
            }

            @Override
            public String name() {
                return "加锁失败";
            }
        };

        /**
         * 异常码
         *
         * @return
         */
        String code();

        /**
         * 异常码说明
         *
         * @return
         */
        String name();
    }

    public ComponentException(ErrCode errCode) {
        this(errCode, "");
    }

    public ComponentException(ErrCode errCode, String message) {
        this(errCode, message, null);
    }

    public ComponentException(ErrCode errCode, Throwable cause) {
        this(errCode, "", cause);
    }

    public ComponentException(ErrCode errCode, String message, Throwable cause) {
        super("ERROR CODE: " + errCode.code() + "[" + errCode.name() + "]" + message, cause); // NOI18N
        this.errCode = errCode;
    }

    public final String getCode() {
        return errCode.code();
    }
    public final String getErrCode() {
        return errCode.toString();
    }

}
